<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="任务单号" prop="workPlanId">
        <el-input v-model="formData.workPlanId" placeholder="请输入任务单号" />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="formData.productName" placeholder="请输入产品名称" />
      </el-form-item>
      <el-form-item label="客户ID" prop="customerId">
        <el-input v-model="formData.customerId" placeholder="请输入客户ID" />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="formData.customerName" placeholder="请输入客户名称" />
      </el-form-item>
      <el-form-item label="计量单位名称" prop="unitName">
        <el-input v-model="formData.unitName" placeholder="请输入计量单位名称" />
      </el-form-item>
      <el-form-item label="计量单位编号" prop="unitId">
        <el-input v-model="formData.unitId" placeholder="请输入计量单位编号" />
      </el-form-item>
      <el-form-item label="客户料号" prop="customerProductId">
        <el-input v-model="formData.customerProductId" placeholder="请输入客户料号" />
      </el-form-item>
      <el-form-item label="产品机种" prop="machineType">
        <el-select v-model="formData.machineType" placeholder="请选择产品机种">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="机种件别" prop="machineItem">
        <el-input v-model="formData.machineItem" placeholder="请输入机种件别" />
      </el-form-item>
      <el-form-item label="产品简称" prop="productShortName">
        <el-input v-model="formData.productShortName" placeholder="请输入产品简称" />
      </el-form-item>
      <el-form-item label="生产组编码" prop="productGroupCode">
        <el-input v-model="formData.productGroupCode" placeholder="请输入生产组编码" />
      </el-form-item>
      <el-form-item label="任务单类型" prop="workPlanType">
        <el-select v-model="formData.workPlanType" placeholder="请选择任务单类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MES_WORK_PLAN_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务单类型编码" prop="workPlanTypeCode">
        <el-input v-model="formData.workPlanTypeCode" placeholder="请输入任务单类型编码" />
      </el-form-item>
      <el-form-item label="车间ID" prop="workShopId">
        <el-input v-model="formData.workShopId" placeholder="请输入车间ID" />
      </el-form-item>
      <el-form-item label="车间名称" prop="workShopName">
        <el-input v-model="formData.workShopName" placeholder="请输入车间名称" />
      </el-form-item>
      <el-form-item label="车间名称编码" prop="workShopCode">
        <el-input v-model="formData.workShopCode" placeholder="请输入车间名称编码" />
      </el-form-item>
      <el-form-item label="计划产量" prop="workPlanProductNum">
        <el-input v-model="formData.workPlanProductNum" placeholder="请输入计划产量" />
      </el-form-item>
      <el-form-item label="计划开始时间" prop="planStartTime">
        <el-date-picker
          v-model="formData.planStartTime"
          type="date"
          value-format="x"
          placeholder="选择计划开始时间"
        />
      </el-form-item>
      <el-form-item label="计划结束时间" prop="planEndTime">
        <el-date-picker
          v-model="formData.planEndTime"
          type="date"
          value-format="x"
          placeholder="选择计划结束时间"
        />
      </el-form-item>
      <el-form-item label="实际开始时间" prop="actualStartTime">
        <el-date-picker
          v-model="formData.actualStartTime"
          type="date"
          value-format="x"
          placeholder="选择实际开始时间"
        />
      </el-form-item>
      <el-form-item label="实际结束时间" prop="actualEndTime">
        <el-date-picker
          v-model="formData.actualEndTime"
          type="date"
          value-format="x"
          placeholder="选择实际结束时间"
        />
      </el-form-item>
      <el-form-item label="工单状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="发料人编号" prop="distributeManagerId">
        <el-input v-model="formData.distributeManagerId" placeholder="请输入发料人编号" />
      </el-form-item>
      <el-form-item label="收料人编号" prop="receiveManagerId">
        <el-input v-model="formData.receiveManagerId" placeholder="请输入收料人编号" />
      </el-form-item>
      <el-form-item label="模具编号" prop="moldNo">
        <el-input v-model="formData.moldNo" placeholder="请输入模具编号" />
      </el-form-item>
      <el-form-item label="模穴数量" prop="moldCaveNum">
        <el-input v-model="formData.moldCaveNum" placeholder="请输入模穴数量" />
      </el-form-item>
      <el-form-item label="切换方式" prop="exchangeType">
        <el-select v-model="formData.exchangeType" placeholder="请选择切换方式">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否模具共用" prop="isMoldSame">
        <el-radio-group v-model="formData.isMoldSame">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="包规数量" prop="quantity">
        <el-input v-model="formData.quantity" placeholder="请输入包规数量" />
      </el-form-item>
      <el-form-item label="制单时间" prop="generateDateTime">
        <el-date-picker
          v-model="formData.generateDateTime"
          type="date"
          value-format="x"
          placeholder="选择制单时间"
        />
      </el-form-item>
      <el-form-item label="制单人" prop="generateUser">
        <el-input v-model="formData.generateUser" placeholder="请输入制单人" />
      </el-form-item>
      <el-form-item label="结案时间" prop="closeDateTime">
        <el-date-picker
          v-model="formData.closeDateTime"
          type="date"
          value-format="x"
          placeholder="选择结案时间"
        />
      </el-form-item>
      <el-form-item label="任务单内码" prop="workPlanInterId">
        <el-input v-model="formData.workPlanInterId" placeholder="请输入任务单内码" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { BaseWorkPlanApi, BaseWorkPlanVO } from '@/api/mes/baseworkplan'

/** 任务单信息 表单 */
defineOptions({ name: 'BaseWorkPlanForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  workPlanId: undefined,
  productName: undefined,
  customerId: undefined,
  customerName: undefined,
  unitName: undefined,
  unitId: undefined,
  customerProductId: undefined,
  machineType: undefined,
  machineItem: undefined,
  productShortName: undefined,
  productGroupCode: undefined,
  workPlanType: undefined,
  workPlanTypeCode: undefined,
  workShopId: undefined,
  workShopName: undefined,
  workShopCode: undefined,
  workPlanProductNum: undefined,
  planStartTime: undefined,
  planEndTime: undefined,
  actualStartTime: undefined,
  actualEndTime: undefined,
  status: undefined,
  distributeManagerId: undefined,
  receiveManagerId: undefined,
  moldNo: undefined,
  moldCaveNum: undefined,
  exchangeType: undefined,
  isMoldSame: undefined,
  quantity: undefined,
  generateDateTime: undefined,
  generateUser: undefined,
  closeDateTime: undefined,
  workPlanInterId: undefined,
})
const formRules = reactive({
  workPlanId: [{ required: true, message: '任务单号不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BaseWorkPlanApi.getBaseWorkPlan(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BaseWorkPlanVO
    if (formType.value === 'create') {
      await BaseWorkPlanApi.createBaseWorkPlan(data)
      message.success(t('common.createSuccess'))
    } else {
      await BaseWorkPlanApi.updateBaseWorkPlan(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    workPlanId: undefined,
    productName: undefined,
    customerId: undefined,
    customerName: undefined,
    unitName: undefined,
    unitId: undefined,
    customerProductId: undefined,
    machineType: undefined,
    machineItem: undefined,
    productShortName: undefined,
    productGroupCode: undefined,
    workPlanType: undefined,
    workPlanTypeCode: undefined,
    workShopId: undefined,
    workShopName: undefined,
    workShopCode: undefined,
    workPlanProductNum: undefined,
    planStartTime: undefined,
    planEndTime: undefined,
    actualStartTime: undefined,
    actualEndTime: undefined,
    status: undefined,
    distributeManagerId: undefined,
    receiveManagerId: undefined,
    moldNo: undefined,
    moldCaveNum: undefined,
    exchangeType: undefined,
    isMoldSame: undefined,
    quantity: undefined,
    generateDateTime: undefined,
    generateUser: undefined,
    closeDateTime: undefined,
    workPlanInterId: undefined,
  }
  formRef.value?.resetFields()
}
</script>